#ifndef SSLOPERATION_H
#define SSLOPERATION_H

#include <QObject>
#include <QSslSocket>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QVariant>
#include <QEventLoop>
#include <QHash>

QT_BEGIN_NAMESPACE
class QNetworkReply;
QT_END_NAMESPACE



class SslOperation : public QObject
{
    Q_OBJECT
public:

    enum Method{
        NONE,
        CONNECT,
        GET,
        POST,
        CLOSE
    };

    enum ContentRequstType{
        PercentEncoding,
        JSONRequest
    };

    enum ContentAnswerType{
        HTML,
        JSONAnswer
    };

    struct ContentParameter{
        ContentParameter();
        ContentParameter(QString _key, QString _value, QString _hash_key);
        QString     key;
        QString     value;
        QString     hash_key;
        ContentParameter& operator=(const ContentParameter &other);
        bool operator==(const ContentParameter &other);
    };

    struct OperationContetnt{
        OperationContetnt();
        OperationContetnt( Method _method, QString _address,
                          QList<ContentParameter> _ReceiveParamenters=QList<ContentParameter>(),
                          QList<ContentParameter> _SendParamenters=QList<ContentParameter>(),
                          ContentAnswerType _answerType=HTML,
                          ContentRequstType _requestType=PercentEncoding,
                          QString _errorString="");
        QString                 address;
        Method                  method;
        QString                 errorString;
        QList<ContentParameter> ReceiveParamenters;
        ContentAnswerType       answerType;
        QList<ContentParameter> SendParamenters;
        ContentRequstType       requestType;
        OperationContetnt& operator=(const OperationContetnt &other);
        bool operator==(const OperationContetnt &other);
    };

    explicit    SslOperation(QObject *parent = 0);
    explicit    SslOperation(QList<OperationContetnt> *operList, QObject *parent = 0);
    ~SslOperation();
    /*void        DoOperation(Method method,
                            QList<QPair<QString, QString>* > *in_content,
                            QList<QPair<QString, QString>* > *out_content,
                            ContentAnswerType answType=HTML,
                            ContentRequstType reqType=PercentEncoding,
                            QSslSocket *socket=0);*/
    void        Engine();
//    void        DoCONNECT(QString host);
//    void        DoGET(QString address,
//                      QList<QPair<QString, QString>* > *received_content,
//                      ContentAnswerType answType=HTML);
//    void        DoPOST(QString address,
//                       QList<QPair<QString, QString>* > *send_content,
//                       QList<QPair<QString, QString>* > *received_content,
//                       ContentAnswerType answType=HTML,
//                       ContentRequstType reqType=PercentEncoding);
    void        DoCONNECT();
    void        DoGET();
    void        DoPOST();

    void        DoCLOSE();

    void        SetOperList(QList<OperationContetnt> *operList);
    void        SetManager(QNetworkAccessManager   *manager);

    QSslSocket              *m_socket;
    QNetworkAccessManager   m_manager;

signals:
    void        JobFinished();
    void        OperationFinished(QVariant , QList<QPair<QString, QString> *>*, QSslSocket *);
    void        OperFinished();
    void        Error(QString );
    void        Errors(QList<QSslError>);

public slots:

private slots:
    void        Encrypted(QNetworkReply *reply);
    void        socketError(QAbstractSocket::SocketError error);
    void        SslSocketError(QList<QSslError> errList);
    void        sslErrorsSlot(QNetworkReply * reply, const QList<QSslError> & errors);
    void        finishedGetRequestSlot(QNetworkReply* reply);
    void        finishedGetRequestSlot2(QNetworkReply* reply);
    void        finishedPostRequestSlot(QNetworkReply*reply);
    void        finishedOper();
private:
    Method      m_method;
    ContentRequstType m_contentRequestType;
    ContentAnswerType m_contentAnswerType;
    QList<OperationContetnt> *m_operList;
    int         m_currentOperation;
    bool        m_finishedOperation;
    QHash<QString, QString>          m_hash;
//    QList<QPair<QString, QString>* > *m_inParameters;
//    QList<QPair<QString, QString>* > *m_outParameters;
//    bool        (*ParsePostError)(QByteArray &reply);
    void        ParseAnswer(QByteArray &bytes);
    QString     TakeAddress(QString adress_string);

    //QEventLoop  m_loop;
    QSsl::SslProtocol       m_protocol;
//    QString     m_hostName;
//    QString     lastUrl;
};

#endif // SSLOPERATION_H
